using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;

namespace ZYFramework.ObjectPool
{

    /// <summary>
    /// 对象池核心接口，支持同步和异步操作
    /// </summary>
    /// <typeparam name="T">池化对象类型</typeparam>
    public interface IObjectPool<T>
    {
        /// <summary>
        /// 同步获取对象
        /// </summary>
        /// <param name="priority">优先级</param>
        /// <returns>池化对象</returns>
        T Get_Sync();

        /// <summary>
        /// 异步获取对象
        /// </summary>
        /// <param name="priority">优先级</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>池化对象</returns>
        UniTask<T> Get_Async(CancellationToken cancellationToken = default);

        /// <summary>
        /// 同步释放对象
        /// </summary>
        /// <param name="obj">要释放的对象</param>
        void Release_Sync(Object obj);

        /// <summary>
        /// 异步释放对象
        /// </summary>
        /// <param name="obj">要释放的对象</param>
        /// <param name="cancellationToken">取消令牌</param>
        UniTask Release_Async(Object obj, CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量获取对象
        /// </summary>
        /// <param name="count">获取数量</param>
        /// <param name="priority">优先级</param>
        /// <returns>对象数组</returns>
        T[] GetRange_Sync(int count);

        /// <summary>
        /// 批量释放对象
        /// </summary>
        /// <param name="objects">要释放的对象集合</param>
        void ReleaseRange_Sync(List<T> objects);

        /// <summary>
        /// 异步批量获取对象
        /// </summary>
        /// <param name="count">获取数量</param>
        /// <param name="priority">优先级</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>对象数组</returns>
        UniTask<T[]> GetRange_Async(int count, CancellationToken cancellationToken = default);

        /// <summary>
        /// 异步批量释放对象
        /// </summary>
        /// <param name="objects">要释放的对象集合</param>
        /// <param name="cancellationToken">取消令牌</param>
        UniTask ReleaseRange_Async(List<T> objects, CancellationToken cancellationToken = default);

        /// <summary>
        /// 同步清理对象池
        /// </summary>
        void Clear_Sync();

        /// <summary>
        /// 异步清理对象池
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        UniTask Clear_Async(CancellationToken cancellationToken = default);

        /// <summary>
        /// 对象创建事件
        /// </summary>
        event Action<T> OnObjectCreated;

        /// <summary>
        /// 对象获取事件
        /// </summary>
        event Action<T> OnObjectAcquired;

        /// <summary>
        /// 对象释放事件
        /// </summary>
        event Action<T> OnObjectReleased;

        /// <summary>
        /// 对象销毁事件
        /// </summary>
        event Action<T> OnObjectDestroyed;

        /// <summary>
        /// 池回收事件
        /// </summary>
        event Action OnPoolRecycled;

        /// <summary>
        /// 池溢出事件
        /// </summary>
        event Action OnPoolOverflow;

        /// <summary>
        /// 检查对象是否在活跃对象池中
        /// </summary>
        /// <param name="obj">要检查的对象</param>
        /// <returns>是否在活跃对象池中</returns>
        bool IsObjectInActivePool(Object obj);

        /// <summary>
        /// 检查对象是否在对象池中
        /// </summary>
        /// <param name="obj">要检查的对象</param>
        /// <returns>是否在对象池中</returns>
        bool IsObjectInPool(Object obj);

        #region 统计

        int GetActiveCount();
        int GetIdleCount();
        int GetTotalCount();

        #endregion
    }
}
